package io.framework.modules.air.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.DateUtils;
import io.framework.modules.air.dao.AirDayNumberDao;
import io.framework.modules.air.dto.AirDayNumberDTO;
import io.framework.modules.air.entity.AirDayNumberEntity;
import io.framework.modules.air.service.AirDayNumberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 优良天数统计
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-21
 */
@Service
public class AirDayNumberServiceImpl extends CrudServiceImpl<AirDayNumberDao, AirDayNumberEntity, AirDayNumberDTO> implements AirDayNumberService {

    @Override
    public QueryWrapper<AirDayNumberEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AirDayNumberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public List<Map<String,Object>> youLiangNumber(Map<String, Object> params){
        List<Map<String,Object>> mapList = new ArrayList<>();
        Date date = DateUtils.parse(params.get("date").toString(),DateUtils.DATE_MONTH_PATTERN);
        Map<String ,Object> map = null;
        //获取数据
        List<AirDayNumberEntity> list= baseDao.youLiangNumber(params);
        AirDayNumberEntity entity = null;
        if(CollectionUtils.isNotEmpty(list)){
            entity = list.get(0);
        }else{
            return  mapList;
        }
        if("1".equals(params.get("category")+"")){
            //同比  上一年当月数据
            map = new HashMap<>(5);
            map.put("date",DateUtils.format(DateUtils.addDateYears(date,-1),DateUtils.DATE_MONTH_PATTERN));
            map.put("userFul",entity.getUserfulTongbi());
            map.put("you",entity.getYouTongbi());
            map.put("liang",entity.getLiangTongbi());
            map.put("qingd",entity.getQingdTongbi());
            mapList.add(map);
        }else{
            //环比  上一月数据
            map = new HashMap<>(5);
            map.put("date",DateUtils.format(DateUtils.addDateMonths(date,-1),DateUtils.DATE_MONTH_PATTERN));
            map.put("userFul",entity.getUserfulHuanbi());
            map.put("you",entity.getYouHuanbi());
            map.put("liang",entity.getLiangHuanbi());
            map.put("qingd",entity.getQingdHuanbi());
            mapList.add(map);
        }
        map = new HashMap<>(5);
        map.put("date",DateUtils.format(date,DateUtils.DATE_MONTH_PATTERN));
        map.put("userFul",entity.getUserfulNum());
        map.put("you",entity.getYouNum());
        map.put("liang",entity.getLiangNum());
        map.put("qingd",entity.getQingdNum());
        mapList.add(map);
        return mapList;
    }

    @Override
    public List<Map<String,Object>> youLiangNumberMonth(Map<String, Object> params){
        List<Map<String,Object>> mapList = new ArrayList<>(3);
        Map<String ,Object> map = new HashMap<>(0);
        //获取数据
        List<AirDayNumberEntity> list= baseDao.youLiangNumber(params);
        AirDayNumberEntity entity = null;
        if(CollectionUtils.isNotEmpty(list)){
            entity = list.get(0);
        }else{
            return  mapList;
        }
        map = new HashMap<>(2);
        map.put("name","优级");
        map.put("value",entity.getYouNum());
        mapList.add(map);
        map = new HashMap<>(2);
        map.put("name","良级");
        map.put("value",entity.getLiangNum());
        mapList.add(map);
        map = new HashMap<>(2);
        map.put("name","轻度污染");
        map.put("value",entity.getQingdNum());
        mapList.add(map);
        return mapList;
    }

    @Override
    public Boolean checkData(Integer type, String month) {
        Boolean flag = false;
        List<AirDayNumberEntity> list = baseDao.checkData(type,month);
        if(CollectionUtils.isNotEmpty(list)){
            flag = true;
        }
        return flag;
    }


}